﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.PL.ServiceClient;
using System.Text.RegularExpressions;

namespace Empires.PL.PageBuilder
{
    /// <summary>
    /// 解析核心建造者
    /// </summary>
    public static class Commander
    {
        /// <summary>
        /// 解析指定页面
        /// </summary>
        /// <param name="tempeleName">需要解析的页面名，如需要解析首页就是index.html。不需要加路径</param>
        /// <param name="copName">公司唯一标示名</param>
        /// <returns></returns>
        public static HTMLPack Analysis(string tempeleName, string copName)
        {
            var customerTempletePath = CompanyBaseServiceClient.GetCompanyTempleteBasePath(copName); //获得所用模板路径（\\WebTemplete\\SystemStyle\\LawFirm\\）
            if (!(customerTempletePath.EndsWith("\\") || customerTempletePath.EndsWith("/")))
            {
                customerTempletePath += "\\";
            }
            var basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //应用程序根目录
            if (!(basePath.EndsWith("\\") || basePath.EndsWith("/")))
            {
                basePath += "\\";
            }

            customerTempletePath = String.Format("{0}{1}{2}", basePath, customerTempletePath, tempeleName); //获得模板文件的物理地址
            string textStream = FileLoader.LoadFile(customerTempletePath); //获得文件流
            string head = string.Empty;
            string body = string.Empty;
            Regex reg = new Regex(PageBuilderRegexpLib.HeadPicker);
            if (reg.IsMatch(textStream))//匹配head字段
            {
                head = HeadAnalyzer.Analysis(reg.Match(textStream).Groups[0].ToString(), customerTempletePath, copName).Replace("*r*n", "\r\n");
            }
            reg = new Regex(PageBuilderRegexpLib.BodyPicker);
            if (reg.IsMatch(textStream)) //匹配body字段
            {
                body = BodyAnalyzer.Analysis(reg.Match(textStream).Groups[0].ToString(), customerTempletePath,
                    copName).Replace("*r*n", "\r\n");
            }
            return new HTMLPack() { Body = body, Head = head };
        }
    }
    public class HTMLPack
    {
        public string Head { get; set; }
        public string Body { get; set; }
    }
}
